package com.tao.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tao.pojo.Answerer;
import com.tao.service.AnswererService;
import com.tao.mapper.AnswererMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.*;
import org.springframework.stereotype.Service;

import java.io.Serializable;
import java.util.List;

/**
 *
 */
@Service
@CacheConfig(cacheNames = "AnswererTable")
public class AnswererServiceImpl extends ServiceImpl<AnswererMapper, Answerer>
    implements AnswererService{

    Logger logger = LoggerFactory.getLogger(getClass());

    @Autowired
    AnswererMapper answererMapper;

    @Cacheable(key ="'Answerer::all'")
    @Override
    public List<Answerer> selectAll() {
//        查出来不包含密码
        QueryWrapper<Answerer> queryWrapper = new QueryWrapper<>();
        queryWrapper.select(Answerer.class, info -> !info.getColumn().equals("password"));
//        !info.getColumn().equals("manager_id") && !info.getColumn().equals("create_time")

        return answererMapper.selectList(queryWrapper);
    }


//    @CacheEvict(value="AnswererTable",key ="'AllTable'")
//    @CachePut

    @Override
    @Cacheable(key ="'Answerer::'+#p0")
    public Answerer selectByid(Long id){
        QueryWrapper<Answerer> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id",id);
        queryWrapper.select(Answerer.class, info -> !info.getColumn().equals("password"));
        return answererMapper.selectById(id);
    }

    @Override
    @Cacheable(key ="'Answerer::Byname.'+#p0")
    public Answerer selectByUsername(String username) {
        QueryWrapper<Answerer> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("name",username);
        return answererMapper.selectOne(queryWrapper);
    }


    @Override
//    @Cacheable(key ="'Answerer::'+answerer.id")
    @Caching(evict = {
            @CacheEvict(value ="AnswererTable::Answerer::pages",allEntries = true),
            @CacheEvict(key ="'Answerer::Byname.'+#p0.name"),
    })
    public boolean addAnswerer(Answerer answerer) {
//        System.out.println(answerer);
        return super.save(answerer);
    }

    @Cacheable(key ="'Answerer::pages::page'+#p0.getCurrent()")
    @Override
    public  List<Answerer> selectPageVo(Page<?> page) {
//        第2页     每页两个
//        IPage<Answerer> answererPage = new Page<>(2, 2);

        IPage<Answerer> answererPage = (IPage<Answerer>) page;

//        对象绑定分页
        //        查出来不包含密码
        QueryWrapper<Answerer> queryWrapper = new QueryWrapper<>();
        queryWrapper.select(Answerer.class, info -> !info.getColumn().equals("password"));
        answererPage=answererMapper.selectPage(answererPage,queryWrapper);
        List<Answerer> records = answererPage.getRecords();

        return records;
    }

    @Cacheable(value ="AnswererTable::Answerer::pages",key ="'pageCount'")
    @Override
    public int getCount() {
        return answererMapper.getCount();
    }


    //    添加一个回答数量
    @Override
//    @CacheEvict(key ="'Answerer::all'")
    //这里要清除多个key就不能只用 @CacheEvict
    @Caching(evict = {
            @CacheEvict(key ="'Answerer::all'"),
            @CacheEvict(key ="'Answerer::'+#p0"),
    })
    public int addAnswerSum(Long id) {
        Answerer answerer = answererMapper.selectById(id);
//        System.out.println(answerer);
        logger.info(answerer.toString());

        return answererMapper.addAnswerSum(answerer);
    }

    @Override
    @Cacheable(key = "'Answerer.'+#p0.id")
    public boolean save(Answerer entity) {
        return super.save(entity);
    }


    @Override
    @CacheEvict(key = "'Answerer.'+#p0")
    public boolean removeById(Serializable id) {
        return super.removeById(id);
    }

    @Override
    @Caching(
            evict = {
                    @CacheEvict(key ="'Answerer::all'"),
                    @CacheEvict(key ="'Answerer::'+#p0.id"),
            }
    )
    public boolean update(Answerer entity, Wrapper<Answerer> updateWrapper) {
        return super.update(entity, updateWrapper);
    }
}




